<?php

/**
 *   52zll.top
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
if(!$_G['uid'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
	$id = in_array('dzapp_wxlogin', $_G['setting']['plugins']['available']) ? 'dzapp_wxlogin' : 'dzapp_zhibo';
	$url = 'plugin.php?id='.$id.'&mod=oauth&referer='.urlencode($_G['siteurl'].'plugin.php?id=dzapp_zhibo&mod=view&identify='.$_GET['identify']);
	dheader("Location: $url");
}

$zhibo = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->fetch($_GET['identify']);
$tab_num = $zhibo['tab_title'] ? 3 : 2;

$navtitle = $zhibo['name'];

if(empty($zhibo)) {
	showmessage('nonexistence');
}

$in_wx = strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger');

if($_GET['paysubmit'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
	
	if($_GET['formhash'] != $_G['formhash']) {
		showmessage('submit_invalid');
	}
	
	$gift = C::t('#dzapp_zhibo#dzapp_zhibo_gift')->fetch($_GET['gift_id']);
	$setarr = array(
		'activity_id' => $zhibo['activity_id'],
		'uid' => $_G['uid'],
		'username' => $_G['username'],
		'gift_id' => $_GET['gift_id'],
		'price' => $gift['price'],
		'dateline' => $_G['timestamp'],
		'ip' => $_G['ip'],
	);

	$order_id = C::t('#dzapp_zhibo#dzapp_zhibo_order')->insert($setarr, true);
	
	$param = array(
		'subject' =>	$navtitle,
		'body' =>		'cessssssss',
		'show_url' =>	'plugin.php?id=dzapp_zhibo&mod=view&identify='.$_GET['identify'],
		'price' =>		$gift['price'],
		'order_id' =>	'zhibo'.$order_id,
		'return' =>		'plugin.php?id=dzapp_zhibo&mod=view&identify='.$_GET['identify'],
	);
	echo pay_wxpay($param);exit;
}else{
	//exit('yy');
	wx_init('plugin.php?id=dzapp_zhibo&mod=view&identify='.$_GET['identify']);
}

$manager = array_filter(explode(',', $zhibo['manager']));
$zhibo['advert'] = $zhibo['advert'] ? dzapp_pic_get($zhibo['advert'], 'portal', 0, $_G['setting']['ftp']['on']) : '';

$perpage = $var['perpage'] ? $var['perpage'] : 10;

$get = array(
	'id' => 'dzapp_zhibo',
	'mod' => 'view',
	'ac' => $ac
);


if($zhibo['end'] || $_G['timestamp'] >= strtotime($zhibo['end_time'])){
	
	require DISCUZ_ROOT.'./source/plugin/dzapp_zhibo/class/zhiboController.php';
	$obj = new zhiboController;
	$obj->zhibo_ver = '3.1';
	
	$param = array(
		array('activity_id', $_GET['identify']),
	);
	$query = C::t('#dzapp_zhibo#dzapp_zhibo_live')->fetch_all_by_search($param);
	foreach($query as $value) {
		
		if($value['liveid']){
			
			$param = array('liveId' => $value['liveid']);
			$rs = $obj->zhibo('lecloud.cloudlive.rec.searchResult', $param, 'get');
			if($rs['rows']){
				$video = $rs['rows'][0];
				break;
			}
		}
	}
}

$gift = C::t('#dzapp_zhibo#dzapp_zhibo_gift')->fetch_all_by_search();
foreach($gift as $value){
	$value['icon'] = $value['icon'] ? dzapp_pic_get($value['icon'], 'portal', 0, $_G['setting']['ftp']['on']) : 'static/image/common/nophoto.gif';
	$glist[] = $value;
}

$type = $_G['mobile'] ? 'wap' : 'pc';
$diy = $_G['mobile'] ? '' : 'diy:';
$path = $_G['cache']['dzapp_template'][$identifier][$type] ? $_G['cache']['dzapp_template'][$identifier][$type] : 'source/plugin/'.$identifier.'/template';

$_G['weixin']['title'] = $zhibo['name'];
$_G['weixin']['link'] = $_G['siteurl'].str_replace('/', '', $_SERVER['REQUEST_URI']);
$_G['weixin']['imgUrl'] = $_G['siteurl'].dzapp_pic_get($zhibo['cover_img_url'], 'portal');
$_G['weixin']['desc'] = cutstr(strip_tags($zhibo['tab_content']), 200);

include template($diy.substr($identifier, 6).'_'.$mod, 0, $path);
//include template('diy:zhibo_view', 0, 'source/plugin/dzapp_zhibo/template/touch');
?>